package flatlandRPG;

import org.lwjgl.util.Color;

/**
 * A 2D colored Polygon.
 *
 * @author trev
 */
public class GLColoredPolygon
    extends GLPolygon
{
    /**
     * Create a new GLPolygon object with the given points in a clockwise
     * direction. It takes 3 floats for the rgb values and a list of points.
     *
     * @param pointsAndColors
     *            a list of the points and colors for the polygon.
     */
    public GLColoredPolygon(Float... pointsAndColors)
    {
        super(getPoints(pointsAndColors));
        int[] colors = getColors(pointsAndColors);
        setColor(new Color(colors[0], colors[1], colors[2]));
    }


    /**
     * Gets the points of the polygon from a Float array with points & colors.
     *
     * @param pointsAndColors
     *            Contains points and colors for a polygon
     * @return Array consisting of only points for the polygon
     */
    private static Float[] getPoints(Float... pointsAndColors)
    {
        Float[] points = new Float[pointsAndColors.length - 3];
        for (int i = 3; i < pointsAndColors.length; i++)
        {
            points[i - 3] = pointsAndColors[i];
        }
        return points;
    }


    /**
     * Gets the colors of the polygon from a Float array with points & colors.
     *
     * @param pointsAndColors
     *            Contains points and colors for a polygon
     * @return Array consisting of only colors for the polygon (R,G,B)
     */
    private static int[] getColors(Float... pointsAndColors)
    {
        int[] colors = new int[3];
        for (int i = 0; i < 3; i++)
        {
            colors[i] =
                Integer
                    .parseInt(String.valueOf(Math.round(pointsAndColors[i])));
        }
        return colors;
    }
}
